<?php


namespace app\index\model;



use think\Exception;
use think\Model;


class CustomerTrackRecord extends Model
{
    public static $track_type=['未跟进','需跟进','已预约','完结'];

    public static function getByCustomerId($id){
        $CustomerTrackRecordClass=new self();
        return $CustomerTrackRecordClass->alias('ctr')->join('user u','ctr.create_user=u.id')->where('ctr.order_id',$id)->where('ctr.del_flag',0)->field('ctr.id,ctr.content,ctr.status,ctr.create_time,ctr.appointment_time,u.user_name')->order('create_time desc')->select()->toArray();
    }

    public static function addTrackRecord($id,$track_record,$customer,$end_id){
        $CustomerTrackRecordClass=new self();
        $OrderClass=new Order();

        $CustomerTrackRecordClass->startTrans();
        try{
            if($end_id) {
                $CustomerTrackRecordClass::update(['status' => 1], ['id' => $end_id]);
            }
            $check=$CustomerTrackRecordClass->where('order_id',$id)->where('status',0)->where('del_flag',0)->field('id,customer_id')->select()->toArray();
            if(!empty($check)){
                return ['status'=>false,'result'=>'不能继续添加记录'];
            }

            $result=$CustomerTrackRecordClass->isUpdate(false)->save($track_record);
            if($result===false){
                throw Exception('保存记录失败');
            }

            $result=$OrderClass->isUpdate(true)->save($customer,['id'=>$id]);
            if($result===false){
                throw Exception('保存时间失败');
            }
            $CustomerTrackRecordClass->commit();
            return ['status'=>true,'result'=>'保存成功'];
        }catch (Exception $e){
            $CustomerTrackRecordClass->rollback();
            trace('addTrack '.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'添加失败'];
        }
    }

    public static function endTrackRecord($id,$order_id){
        $CustomerTrackRecordClass=new self();
        $OrderClass=new Order();

        $CustomerTrackRecordClass->startTrans();
        try {
            $result=$CustomerTrackRecordClass->save(['status' => 1],['id'=>$id]);
            if($result===false){
                throw Exception('完结失败');
            }
            $old_order=$OrderClass->where('id',$order_id)->field('track_end_times')->find();
            $result=$OrderClass->isUpdate(true)->save(['last_track_time'=>null,'track_end_times'=>($old_order['track_end_times']+1)],['id'=>$order_id]);
            if($result===false){
                throw Exception('保存时间失败');
            }
            $CustomerTrackRecordClass->commit();
            return ['status'=>true,'result'=>'完结成功'];
        }catch (Exception $e){
            $CustomerTrackRecordClass->rollback();
            trace('endTrack'.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'完结失败'];
        }
    }

    public static function endsTrackRecord($ids){
        $CustomerTrackRecordClass=new self();
        $OrderClass=new Order();

        $CustomerTrackRecordClass->startTrans();
        try {
            $order_data=$OrderClass->where('id','in',$ids)->where('del_flag','=',0)->field('id,track_end_times,last_track_time')->select()->toArray();
            if(empty($order_data)){
                return ['status'=>false,'result'=>'没有需要完结的跟进'];
            }
            $update_order=[];
            foreach ($order_data as $key=>$value){
                if($value['last_track_time']){
                    $update_order[]=['id'=>$value['id'],'track_end_times'=>($value['track_end_times']+1),'last_track_time'=>null];
                }
            }

            $result=$OrderClass->saveAll($update_order);
            if($result===false){
                return ['status'=>false,'result'=>'更新订单失败'];
            }

            $result=$CustomerTrackRecordClass->where('order_id','in',$ids)->where('status','=',0)->update(['status'=>1,'appointment_time'=>null]);
            if($result===false){
                return ['status'=>false,'result'=>'更新跟进失败'];
            }
            $CustomerTrackRecordClass->commit();
            return ['status'=>true,'result'=>'完结成功'];
        }catch (Exception $e){
            $CustomerTrackRecordClass->rollback();
            trace('endTrack'.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'完结失败'];
        }
    }
}